const { result } = require('lodash');

const Controller = require('egg').Controller;
const { getCookieValue } = require('../public/processCookies');
/**
* @controller SrvProfileController 注释必写，swagger-doc是根据这段注释来生成接口的 ）。
*/
class SrvProfileController extends Controller {
  async list() {
       const data = this.ctx.request.body
const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');

     let result = await  this.ctx.service.userManagement.list(data,tid)
           
       this.ctx.body = {
           msg:"",
           status:200,
           result
       }
  }
  async changeUser(){
    // console.log(this.ctx.request.body)
    let result = await this.ctx.service.userManagement.changeUser(this.ctx.request.body)
    this.ctx.body = {
      message:result,
      code:result=='success'?200:500
    }
  }
  async deleteUser(){
    const params = this.ctx.query
    await  this.ctx.service.userManagement.deleteUser(params)
    this.ctx.body = {
      msg:'nnnn', 
     status:200,
     params
    }
  }

  // 创建用户分组
  async createGroup(){
    const params = this.ctx.request.body;
    const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    params.tid = tid;
     let result = {}
    let isHave = await this.ctx.service.userManagement.checkGroupName(params.groupName)
    if(!isHave){
      result = await this.ctx.service.userManagement.createGroup(params);

     }else{
       result = {
         msg:'repeat'
       }
     }

    this.ctx.body = {
      msg: result.msg,
      status: 200,
      data: params,
    };
  }

  // 添加组员
  async addGroup(){
    const params = this.ctx.request.body;
    const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    params.tid = tid;
    const result = await this.ctx.service.userManagement.addGroup(params);
    this.ctx.body = {
      msg: result,
      status:result=='success'? 200:500,
      data: params,
    };
  }

  //分组下拉
  async getGroupList(){
const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');

  let result = await this.ctx.modelmysql.UserGroup.findAll({where:{tid}}) 
    
    this.ctx.body = {
      msg:'success',
      status:200,
      data:result
    }
  } 

  // 用户删除
  async deleteUser(){
let msg = ''
    try {
  await this.ctx.modelmysql.UserManagement.destroy({where:{_id:this.ctx.query.id}})
  msg = 'success'
} catch (error) {
  msg = error
}   
this.ctx.body = {
  msg,
}
  }

  // 添加用户
  async addUser(){
    let msg = ''
const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
 
     this.ctx.request.body.tid = tid
    try {
         await this.ctx.modelmysql.UserManagement.create({...this.ctx.request.body})
         msg = 'success'
    }catch(err){
           msg = err
    }
  this.ctx.body = {
    msg,
  }
  } 

  //绑定ONU
  async bindingONU(){
  let res =   await this.ctx.service.userManagement.bindingONU(this.ctx.request.body)
    this.ctx.body = {
      message:res,
      code:res=='success'?200:500
    }
  }

 async unBindingOUN(){
  let result = await this.ctx.service.userManagement.unBindingOUN(this.ctx.query)

   this.ctx.body = {
      code:result=='success'?200:500
   }
 }
 async changeBind(){
   
 }

  async deleteGroup(){
    let id = this.ctx.query.groupId
    let message = ''
    try{
    await  this.ctx.modelmysql.UserGroup.destroy({where:{id}})
           message = 'success'
    }catch(err){
         message = err
    }
    this.ctx.body = {
      message
    }
  }
  /**
   * 修改用户分组
   */
  
}

module.exports = SrvProfileController;
